package NWish.json
{
	public class OrderDict
	{
		private var _dict:Object = new Object();
		
		private var _keys:Array = [];
		
		public function OrderDict()
		{
		}
		
		public function get length()
		{
			return _keys.lenght;
		}
		
		public function isEmpty():Boolean{
			return (length==0);
		}
		
		
		public function get keys():Array
		{
			var temp:Array = new Array(length);
			var index:int = 0;
			for each(var k:* in _keys)
			{
				temp[index] = k;
				index ++;
			}
			return temp;
		}
		
		public function eachKey(func:Function):void
		{
			for(var k:* in _keys)
			{
				func(k);
			}
		}
		
		public function eachValue(func:Function):void
		{
			for each(var k:* in _keys)
			{
				func(_dict[k]);
			}
		}
		
		public function eachKeyValue(func:Function):void
		{
			for each(var k:* in _keys)
			{
				func(k,_dict[k]);
			}
		}
		
		public function values():Array{
			var temp:Array = new Array(length);
			var index:int = 0;
			for each(var k:* in _keys)
			{
				temp[index] = _dict[k];
				index ++;
			}
			return temp;
		}
		
		public function containsValue(value:*):Boolean
		{
			for each(var i:* in _dict)
			{
				if(i === value){
					return true;
				}
			}
			return false;
		}
		
		public function containsKey(key:*):Boolean
		{
			if(_dict[key] != undefined)
			{
				return true;
			}
			return false;
		}
		
		public function get(key:*):*
		{
			return _dict[key];
		}
		
		public function getValue(key:*):*
		{
			return _dict[key];
		}
		
		public function setValue(key:*, value:*):*
		{
			_dict[key] = value;
		}
		
		public function push(key:*, value:*):*
		{
			if(key == null)
			{
				throw new ArgumentError("cannot put a value with undefined or null key!");
				return undefined;
			}
			/*else if(value == null)
			{
				return remove(key);
			}*/
			else
			{
				var oldValue:* = _dict[key];
				
				if(oldValue != undefined)
				{
					throw new Error('oldValue != undefined');
					remove(key);
				}

				_dict[key] = value;
				_keys.push(key);
				
				return oldValue;
			}
		}
		
		public function remove(k:*):*
		{
			var temp = undefined;
			
			var index = _keys.indexOf(k);
			if(index != -1)
			{
				_keys.splice(index,1)
				temp = _dict[k];
				delete _dict[k];
			}
			return temp;
		}
		
		public function clear():void
		{
			_keys = [];
			_dict = new Object();
		}
		
		public function clone():OrderDict
		{
			var temp:OrderDict = new OrderDict();
			for(var k:* in _keys){
				temp.push(k, _dict[k]);
			}
			return temp;
		}
		
		public function toString():String
		{
			var temp:String = "OrderDict Content:\n";
			for(var i:int=0; i<_keys.length; i++){
				temp += _keys[i] + " -> " + _dict[i] + "\n";
			}
			return temp;
		}
		
	}
}